<template>
  <div class="editor-container">
    <!-- 标题栏 -->
    <div class="header clearFix">
      <h3>{{ articleId ? "编辑文章" : "新增文章" }}</h3>
      <div class="header-actions">
        <el-button @click="handleCancel" class="cancel-btn">取消</el-button>
        <el-button type="primary" @click="handleSubmit">
          <i class="el-icon-s-promotion"></i>提交
        </el-button>
      </div>
    </div>

    <!-- 文章表单 -->
    <el-form
      :model="articleForm"
      ref="articleForm"
      label-width="100px"
      class="article-form"
    >
      <!-- 标题 -->
      <el-form-item label="标题">
        <el-input
          v-model="articleForm.title"
          placeholder="请输入标题"
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>

      <el-form-item label="内容">
        <div class="editor-wrapper">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
          />
          <Editor
            style="height: 500px; overflow-y: auto"
            v-model="articleForm.content"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
          />
        </div>
      </el-form-item>

      <el-form-item label="封面">
        <img
          :src="articleForm.titleImage"
          alt="封面图"
          style="max-width: 200px; max-height: 150px"
        />
      </el-form-item>

      <el-form-item label="摘要">
        <el-input
          type="textarea"
          v-model="articleForm.description"
          placeholder="请输入文章摘要"
          rows="3"
          maxlength="200"
          show-word-limit
        ></el-input>
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="分类">
            <el-select
              v-model="articleForm.categoryId"
              placeholder="请选择分类"
              clearable
            >
              <el-option
                v-for="cat in categories"
                :key="cat.value"
                :label="cat.label"
                :value="cat.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标签">
            <el-select
              v-model="articleForm.tags"
              placeholder="请选择标签"
              multiple
              clearable
            >
              <el-option
                v-for="tag in allTags"
                :key="tag.value"
                :label="tag.label"
                :value="tag.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import Vue from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { Message } from "element-ui";

export default Vue.extend({
  components: { Editor, Toolbar },
  data() {
    return {
      articleForm: {
        title: "",
        categoryId: "",
        tags: [],
        description: "",
        content: " ",
        titleImage: "",
        id: "",
      },

      // 编辑器相关
      editor: null,
      mode: "default",
      toolbarConfig: {
        menus: [
          "bold",
          "italic",
          "underline",
          "fontSize",
          "fontFamily",
          "textColor",
          "link",
          "image",
          "list",
          "justifyLeft",
          "justifyCenter",
          "justifyRight",
        ],
      },
      editorConfig: {
        placeholder: "请输入文章内容...",
      },

      categories: [],
      allTags: [],
      articleId: "",
    };
  },
  created() {
    this.articleId = this.$route.query.articleId;

    this.getFen();
    this.getBiao();

    if (this.articleId) {
      this.getXiug(this.articleId);
    }
  },
  methods: {
    async getFen() {
      try {
        const res = await this.$axios.post("/admin/category/select/list");
        console.log("分类列表：", res);
        this.categories = res.data.data;
        console.log(this.categories);
      } catch (error) {
        console.error("加载分类失败：", error);
        Message.error("加载分类失败");
      }
    },

    // 获取标签列表
    async getBiao() {
      try {
        const res = await this.$axios.post("/admin/tag/select/list");
        console.log("标签列表：", res);
        this.allTags = res.data.data;
      } catch (error) {
        console.error("加载标签失败：", error);
        Message.error("加载标签失败");
      }
    },
    async getlll() {
      let res = await this.$axios.post(
        "/admin/article/update",
        this.articleForm
      );
      console.log(res);
      this.$router.push("/houtai/wenzhang");
    },
    async getXiug(articleId) {
      try {
        const res = await this.$axios.post("/admin/article/detail", {
          articleId: articleId,
        });
        console.log(res);
        const article = res.data.data;
        this.articleForm = {
          title: article.title,
          categoryId: article.categoryId,
          tags: article.tagIds,
          description: article.description,
          content: article.content,
          titleImage: article.titleImage,
          id: article.id,
        };
      } catch (error) {
        console.error("加载文章详情失败：", error);
        Message.error("加载文章详情失败");
      }
    },

    onCreated(editor) {
      this.editor = Object.seal(editor);
    },

    handleSubmit() {
      this.getlll();
    },

    handleCancel() {
      this.$router.push("/houtai/wenzhang");
    },
  },

  beforeDestroy() {
    if (this.editor) {
      this.editor.destroy();
    }
  },
});
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
<style scoped>
.editor-container {
  padding: 20px;
  background-color: #fff;
  margin: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.header {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.header h3 {
  float: left;
  margin: 0;
  font-size: 18px;
  color: #333;
}

.header-actions {
  float: right;
}

.cancel-btn {
  margin-right: 10px;
}

.article-form {
  margin-top: 20px;
}

.editor-wrapper {
  border: 1px solid #e5e7eb;
  border-radius: 4px;
}

.clearFix::after {
  content: "";
  display: table;
  clear: both;
}

@media (max-width: 768px) {
  .editor-container {
    margin: 10px;
    padding: 15px;
  }

  .el-col {
    width: 100% !important;
    margin-bottom: 15px;
  }
}
</style>